<?php
/**
 *
 * Author: TheSmile
 * Date: 2024/11/4 17:11
 * Email: <594652523@qq.com>
 */

namespace App\Exports;

use App\Models\Campus;
use App\Models\CourseDuty;
use App\Models\CourseOrder;
use App\Models\Estimate;
use Encore\Admin\Grid\Exporters\ExcelExporter;
use Maatwebsite\Excel\Concerns\WithColumnWidths;
use Maatwebsite\Excel\Concerns\WithMapping;

class CourseEnroll extends ExcelExporter implements WithMapping, WithColumnWidths
{
    protected $fileName = '课程库.xlsx';

    protected $columns = [
        'id'        => '课程ID',
        'name'      => '课程名称',
        'campus_id' => '校区',
        'score'     => '报名人数',//随便找个模型不用的字段
        'intro'     => '签到人数',//随便找个模型不用的字段
        'is_sug'    => '满意度'//随便找个模型不用的字段
    ];

    public function columnWidths(): array
    {
        return ['B' => 30, 'C' => 40];
    }


    public function map($course): array
    {
        return [
            $course->id,
            $course->name,
            self::getCampus($course),
            self::getEnrollNum($course),
            self::getSignNum($course),
            self::getManScore($course)
        ];
    }

    protected static function getCampus($course)
    {
        return Campus::where('id', $course->campus_id)->value('name');
    }

    protected static function getEnrollNum($course)
    {
        $value = CourseOrder::where('course_id', $course->id)->where('status', '!=', -1)->where('status', '!=', 1)->where('status', '!=', 4)->count();
        return (string)$value ?? "0";
    }

    protected static function getSignNum($course)
    {
        $value = CourseDuty::where('course_id', $course->id)->count();
        return (string)$value ?? "0";
    }

    protected static function getManScore($course)
    {
        $value = Estimate::where('course_id', $course->id)->avg('course_score');
        return max($value, "0");
    }
}
